package at.ac.uniklu.rallyemaster.activities;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import at.ac.uniklu.rallyemaster.R;
import at.ac.uniklu.rallyemaster.RallyeMaster;

public class RallyeMapActivity extends MapActivity {
    
	//Menu Variables 
	static final private int OPTIONS_ITEM = Menu.FIRST;
	
	
	private MyLocationOverlay currentLocationOverlay;
	private MapView mapView;
	 
	private Button startButton;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);
        Bundle b = getIntent().getExtras();
        if(b.getString("Track").length()>0){
           	this.setTitle(this.getTitle()+" - "+b.getString("Track"));
        }
        RallyeMaster.logDebug("Map Loaded");
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);// makes the map able to zoom
        mapView.getController().setZoom(10);
        initMyLocation();
        

        //start Button
        startButton = (Button) findViewById(R.id.startButton);
        startButton.setOnClickListener(new View.OnClickListener() {
			
        	// if the start button is pressed the tracking Activity will start
			public void onClick(View v) {
				Intent intent = new Intent(RallyeMapActivity.this, TrackingActivity.class);
				startActivity(intent);
				RallyeMaster.logDebug("Tracking started");
			}
		});
    }
    
    /**
     * Enabled the Overlay for the Users current position
     */
	private void initMyLocation() {
		currentLocationOverlay = new MyLocationOverlay(this, mapView);
		currentLocationOverlay.enableMyLocation();
		mapView.getOverlays().add(currentLocationOverlay);
		if (currentLocationOverlay.getMyLocation() != null){
			mapView.getController().animateTo(currentLocationOverlay.getMyLocation());
		}
        
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//Create and add new Items to the menu
		MenuItem optionsItem = menu.add(0, OPTIONS_ITEM, Menu.NONE,R.string.itemMenu_options);
		
		optionsItem.setIcon(android.R.drawable.ic_menu_preferences);
		
		return true;
	}
	
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch(item.getItemId()){
			case OPTIONS_ITEM:{
				Intent intent = new Intent(this, PreferencesActivity.class);
				startActivity(intent);
				return true;
			}
		}
		return false;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	
}